/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Vista.fuente.Harabara;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author bastian
 */
public class Panel_VisorPDF extends javax.swing.JPanel {

    /**
     * Creates new form VisorPDF
     */
    
    private int NumeroPaginas;
    private int PaginaActual;
    private String RutaArchivo;
    private Harabara fuente;
    public Panel_VisorPDF() {
       
        fuente = new Harabara();
        initComponents();
        this.NumeroPaginas=1;
        jButton_Anterior.setFont(fuente.getFont(14));
        jButton_Imprimir.setFont(fuente.getFont(14));
        jButton_Siguiente.setFont(fuente.getFont(14));
        jButton_Volver.setFont(fuente.getFont(14));
        jLabel_BuscarCliente.setFont(fuente.getFont(24));
        
    }
    
    private VentanaPrincipal V_Principal;
    
    public void setFrame(JFrame f){
        this.V_Principal= (VentanaPrincipal) f;
    }
    
    public void setRutaArchivo(String file){
        this.RutaArchivo = file;
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    public void CambiarPagina(int caso){
        try {
            
            File file = new File(RutaArchivo); //Ojo con esto, aqui es donde se pone el nombre del archivo pdf, en este caso debe estar en el mismo
                                                            //Ubicacion del archivo pdf
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            FileChannel channel = raf.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,0, channel.size());
            PDFFile pdffile = new PDFFile(buf);
            
            NumeroPaginas = pdffile.getNumPages(); //numero de paginas del documento
            //Pagina Siguiente
            if(caso==1) PaginaActual+=1;
            
            
            //Pagina Anterior
            if(caso==2) PaginaActual-=1;
            
            
            if(PaginaActual<1)
                PaginaActual=1;
            
            else if(PaginaActual>NumeroPaginas) 
                PaginaActual=NumeroPaginas;
            
            // muestra la pagina en la posicion de la variable pagina page channel.size()
            System.out.println("Pagina Actual: "+PaginaActual);
            PDFPage page = pdffile.getPage(PaginaActual);
            
            this.pagePanel1.useZoomTool(false);
            this.pagePanel1.showPage(page);
            
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(V_Principal, "Error de entrada y salida:\n"+ex, "Error", JOptionPane.ERROR_MESSAGE);
        }
        
        this.pagePanel1.repaint();
        this.repaint();
    
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_BuscarCliente = new javax.swing.JLabel();
        jButton_Anterior = new javax.swing.JButton();
        jButton_Siguiente = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        pagePanel1 = new com.sun.pdfview.PagePanel();
        jButton_Imprimir = new javax.swing.JButton();
        jButton_Volver = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(1000, 550));
        setMinimumSize(new java.awt.Dimension(1000, 550));
        setPreferredSize(new java.awt.Dimension(1000, 550));

        jLabel_BuscarCliente.setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N
        jLabel_BuscarCliente.setText("Visor de PDF");

        jButton_Anterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/flechas/atras/24.png"))); // NOI18N
        jButton_Anterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_AnteriorActionPerformed(evt);
            }
        });

        jButton_Siguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/flechas/adelante/24.png"))); // NOI18N
        jButton_Siguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_SiguienteActionPerformed(evt);
            }
        });

        jScrollPane1.setMaximumSize(new java.awt.Dimension(1000, 500));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(1000, 500));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(1000, 500));

        pagePanel1.setMaximumSize(new java.awt.Dimension(1000, 1000));
        pagePanel1.setMinimumSize(new java.awt.Dimension(1000, 1000));
        pagePanel1.setPreferredSize(new java.awt.Dimension(1000, 1000));

        javax.swing.GroupLayout pagePanel1Layout = new javax.swing.GroupLayout(pagePanel1);
        pagePanel1.setLayout(pagePanel1Layout);
        pagePanel1Layout.setHorizontalGroup(
            pagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1000, Short.MAX_VALUE)
        );
        pagePanel1Layout.setVerticalGroup(
            pagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1000, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(pagePanel1);

        jButton_Imprimir.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jButton_Imprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/imprimir/24.png"))); // NOI18N
        jButton_Imprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ImprimirActionPerformed(evt);
            }
        });

        jButton_Volver.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jButton_Volver.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/cruz/24.png"))); // NOI18N
        jButton_Volver.setText("Volver");
        jButton_Volver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_VolverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel_BuscarCliente)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton_Anterior)
                                .addGap(18, 18, 18)
                                .addComponent(jButton_Siguiente)
                                .addGap(18, 18, 18)
                                .addComponent(jButton_Imprimir)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton_Volver))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 959, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(31, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jLabel_BuscarCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton_Imprimir)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton_Anterior)
                        .addComponent(jButton_Siguiente))
                    .addComponent(jButton_Volver))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_AnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_AnteriorActionPerformed
        // TODO add your handling code here:
        this.CambiarPagina(2);
    }//GEN-LAST:event_jButton_AnteriorActionPerformed

    private void jButton_SiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_SiguienteActionPerformed
        // TODO add your handling code here:
        this.CambiarPagina(1);
    }//GEN-LAST:event_jButton_SiguienteActionPerformed

    private void jButton_ImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ImprimirActionPerformed
        // TODO add your handling code here:
        //ENVIAR INFORMACION

        

    }//GEN-LAST:event_jButton_ImprimirActionPerformed

    private void jButton_VolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_VolverActionPerformed
        // TODO add your handling code here:
        V_Principal.removePanel_VisorPDF();

    }//GEN-LAST:event_jButton_VolverActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Anterior;
    private javax.swing.JButton jButton_Imprimir;
    private javax.swing.JButton jButton_Siguiente;
    private javax.swing.JButton jButton_Volver;
    private javax.swing.JLabel jLabel_BuscarCliente;
    private javax.swing.JScrollPane jScrollPane1;
    private com.sun.pdfview.PagePanel pagePanel1;
    // End of variables declaration//GEN-END:variables
}
